import { bg_image } from "./indoorConfig.js";
import {getIndoorUnpaidOrder,getOutdoorUnpaidOrder,carcomeDetail} from "../api/parkingRecords/index.js"
import {wechatAdvancePay,} from "../api/home/index.js"
export default {
	data() {
		return {
			bg_image,
			data: {},
			payInfo: {},
			type: 0, //页面来源,为1来自欠费记录
			indoor: true ,//判断是否是室内的单
			options:{},//页面参数
		}
	},
	onLoad(options) {
		console.log('options1',options)
		this.options = options
	},
	onPullDownRefresh(){//页面下拉刷新
		this.fetchSomething(this.options)
		uni.stopPullDownRefresh();
	},
	onReady(){
		this.fetchSomething(this.options)
	},
	methods: {
		async fetchSomething(options){
			if (options.type) {
				this.type = options.type
				this.indoor = options.indoor === 'true' ? true : false;
				this.getDetailByType(this.indoor, options.id, options.departmentId, options.outdoorProjectId)
			} else {
				this.getDetail(options.id)
			}
		},
		previewImage(urls) {
			uni.previewImage({
				urls
			});
		},
		async getDetailByType(indoor, id, departmentId, outdoorProjectId) {
			//室内
			if (indoor) {
				let res = await getIndoorUnpaidOrder({id:id})
				let data = res.data || {}
				this.data = data
			} else {
				//路侧
				let res = await getOutdoorUnpaidOrder({ orderId: id, outdoorProjectId })
				let data = res.data || {}
				this.data = data
			}

		},
		async getDetail(id) {
			let res = await carcomeDetail({id:id})
			let data = res.data || {}
			this.data = data
		},
		copy() {
			let text = this.data.orderId
			// #ifdef H5
			let textarea = document.createElement("textarea")
			textarea.value = text
			textarea.readOnly = "readOnly"
			document.body.appendChild(textarea)
			textarea.select() // 选中文本内容
			textarea.setSelectionRange(0, text.length)
			uni.showToast({ //提示
				title: '复制成功',
				icon: 'success'
			})
			document.execCommand("copy")
			textarea.remove()
			// #endif
			// #ifndef H5
			uni.setClipboardData({
				data: text, //要被复制的内容
				success: () => { //复制成功的回调函数
					uni.showToast({ //提示
						title: `复制成功`,
						icon: 'success'
					})
				}
			}, true);
			// #endif

		},
		async pay() {
			let ress = await wechatAdvancePay({orderId:this.data.orderId})
			this.payInfo = ress.data || {}
			this.onBridgeReady()
		},
		async onBridgeReady() {
			let Provider =  await uni.getProvider({service: 'payment'})
			let payCanshu = {provider: Provider.provider[0] || 'wxpay',}
			payCanshu = {
				...payCanshu,
				orderInfo:this.payInfo.tradeNo,
			}
			let rePayment = await uni.requestPayment({...payCanshu});
			let flags = rePayment.errMsg == 'requestPayment:ok' && rePayment.resultCode != '6001'
			if(flags) {
				this.fetchSomething(this.options)
			}else{
				this.$u.toast('支付失败');
			}
		}
	}
}
